/************************************************************************/
/*    Author: Scott Bevin         
/************************************************************************/

#pragma once

#include "../XGameEntities/GameEntity.h"
#include "../XMessaging/Message.h"
#include "../XPropertys/PropertyContainer.h"

namespace XF
{
	class EntityComponent: public PropertyContainer
	{
	private:
		static int smComponentSpecificID;
		int mComponentID;

		std::string mComponentName;
		bool mInitialized;
		bool mKillComponent;

		std::string mErrorMessage;

	protected:
		GameEntity *mOwner;


		void SetErrorMessage(std::string val) { mErrorMessage = val; }

		virtual bool OnInitialize(){return true;}
		virtual void OnUpdate(double dT){}
		virtual void OnHandleInput(double dT, XF::Input *input){}
		virtual bool OnHandleMessage(const XF::Message& message){return false;}
		virtual void OnShutdown(){}

	public:
		void MessageSelf(XF::MessageSubject::ENUM sub, void* data = NULL) //chris
		{
			mOwner->MessageSelf(sub,  data);
		}
		void ThrowError(std::string error) //chris i added this so all components can easily send of a error message
		{
			mOwner->PostAMessage(XF::MailingList::ERROR_HANDLERS, XF::MessageSubject::ERROR_MESSAGE, &std::string(mComponentName + " : " +error));
		}
		EntityComponent(GameEntity *owner, std::string idName);
		virtual ~EntityComponent();

		void Initialize();
		void Update(double dT){ OnUpdate(dT); }
		void HandleInput(double dT, XF::Input *input){OnHandleInput(dT, input);}
		bool HandleMessage(const XF::Message& message){ return OnHandleMessage(message); }
		void Shutdown()
		{
			if(mInitialized)//chris
				OnShutdown(); 
			mInitialized = false;
		}

		std::string ComponentName(){return mComponentName;}

		GameEntity* Owner(){ return mOwner; }
		bool Initialized(){return mInitialized;}

		void CopyPropertys(EntityComponent *component);

		void Kill(){mKillComponent = true;}
		bool ShouldKill(){return mKillComponent;}

		int ComponentID(){return mComponentID;}
	};
}
